package com.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.pojo.Customer;
import com.six.pojo.dto.CustomerCapitalDto;
import com.six.pojo.dto.CustomerInfoDto;
import com.six.pojo.vo.CustomerVo;
import com.six.result.Result;
import com.six.result.ResultUtils;
import com.six.service.CustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author luolu
 * @create 2022/3/7 14:46
 */
@Controller
@ResponseBody
@RequestMapping("/customer")
@Api(tags = "客户管理接口")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 添加客户
     * @param customer
     * @return
     */
    @ApiOperation(value = "新增客户数据" ,notes = "新增客户数据")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "请求成功！")
    })
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    //RequestBody 规定为json格式的数据
    public Result add(@RequestBody @ApiParam(name = "Customer", value = "新增的客户字段")Customer customer){
        customerService.save(customer);
        return ResultUtils.buildSuccess();
    }


    /**
     * 根据id删除客户数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除客户" ,notes = "根据id删除客户")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "请求成功！")
    })
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result delete(@RequestParam @ApiParam(name = "id", value = "客户的id")Long id){
        customerService.removeById(id);
        return ResultUtils.buildSuccess();
    }


    /**
     * 客户列表查询
     * @param customerVo
     * @return
     */
    @ApiOperation(value = "客户列表查询" ,notes = "客户列表查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "customerCode",value = "客户编号",required = false,paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "customerName",value = "客户名称",required = false,paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "memoCode",value = "助记码",required = false,paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "jiesuanfangshi",value = "结算方式",required = false,paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "relationLevel",value = "关系深度",required = false,paramType = "query",dataType = "string")
    }
    )
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "请求成功！")
    })
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(CustomerVo customerVo){
        //先拿到page对象
        Page<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        LambdaQueryWrapper<Customer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //lambdaQueryWrapper进行条件查询
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(customerVo.getCustomerCode()),Customer::getCustomerCode,customerVo.getCustomerCode()).like(!StringUtils.isEmpty(customerVo.getCustomerName()),Customer::getCustomerName,customerVo.getCustomerName()).eq(!StringUtils.isEmpty(customerVo.getMemoCode()),Customer::getMemoCode,customerVo.getMemoCode()).eq(!StringUtils.isEmpty(customerVo.getJiesuanfangshi()),Customer::getJiesuanfangshi,customerVo.getJiesuanfangshi()).eq(!StringUtils.isEmpty(customerVo.getRelationLevel()),Customer::getRelationLevel,customerVo.getRelationLevel());
        Page<Customer> customerPage = customerService.page(page, lambdaQueryWrapper);

        return ResultUtils.buildSuccess(customerPage.getTotal(),customerPage.getRecords());
    }


    @ApiOperation(value = "根据id修改客户信息" ,notes = "根据id修改客户信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody @ApiParam(name = "Customer", value = "修改的客户字段") Customer customer){
        customerService.updateById(customer);
        return ResultUtils.buildSuccess();
    }


    /**
     * 根据客户id查询客户和联系人信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据客户id查询客户和联系人信息" ,notes = "根据客户id查询客户和联系人信息")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "请求成功！")
    })
    @RequestMapping(value = "/selectInfoById",method = RequestMethod.GET)
    public Result selectCustomerInfo(@RequestParam @ApiParam(name = "id", value = "客户的id") Long id){
        //先拿到page对象
        Page page = new Page();
        Page<CustomerInfoDto> customerInfoDtoPage = customerService.findCustomerInfoPage(page, id);
        return ResultUtils.buildSuccess(customerInfoDtoPage.getTotal(),customerInfoDtoPage.getRecords());
    }


    /**
     * 根据客户id查询客户和财务情况信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据客户id查询客户和财务情况信息" ,notes = "根据客户id查询客户和财务情况信息")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "请求成功！")
    })
    @RequestMapping(value = "/selectCapitalById",method = RequestMethod.GET)
    public Result selectCustomerCapital(@RequestParam @ApiParam(name = "id", value = "客户的id") Long id){
        //先拿到page对象
        Page page = new Page();
        Page<CustomerCapitalDto> customerCapitalDtoPage = customerService.findCustomerCapitalPage(page, id);
        return ResultUtils.buildSuccess(customerCapitalDtoPage.getTotal(),customerCapitalDtoPage.getRecords());
    }

}
